package com.bw.mvvm_comom.mvvm;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.model.BaseModel;
import com.bw.mvvm_comom.utils.ISConnectUtils;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseModel> extends ViewModel implements IViewModel, Observer<BaseEntity> {
    protected M m;
    protected CompositeDisposable compositeDisposable;
    private UIChange uiChange;

    @Override
    public void onCreate() {
        compositeDisposable = new CompositeDisposable();
        initModel();
    }

    @Override
    public void onDestroy() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    private void initModel(){
        ParameterizedType parameterizedType = (ParameterizedType)getClass().getGenericSuperclass();
        Type[] types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[0];
        try {
            m = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    protected abstract void result(BaseEntity baseEntity);

    @Override
    public void onSubscribe(@NotNull Disposable d) {
        compositeDisposable.add(d);
        showDialog();
    }

    @Override
    public void onNext(@NotNull BaseEntity baseEntity) {
        result(baseEntity);
    }

    @Override
    public void onError(@NotNull Throwable e) {
        showMsg("问题出错："+e.getMessage());
    }

    @Override
    public void onComplete() {
        hideDialog();
    }

    public UIChange getUiChange(){
        if (uiChange == null){
            uiChange = new UIChange();
        }
        return uiChange;
    }

    protected void showDialog(){
        getUiChange().setShowDialogObservable(""+System.currentTimeMillis());

    }
    protected void hideDialog(){
        getUiChange().setHideDialogObservable(""+System.currentTimeMillis());
    }
    protected void finishPage(){
        getUiChange().setFinishActObServAble(""+System.currentTimeMillis());
    }
    protected void showMsg(String msg){
        getUiChange().setShowMsgObservable(msg);
    }

    protected void startPage(HashMap<String,Object> map){
        getUiChange().setStartPageObServAble(map);
    }

    public class UIChange{
        private MutableLiveData<String> showDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String> hideDialogObservable = new MutableLiveData<>();
        private MutableLiveData<String> showMsgObservable = new MutableLiveData<>();
        private MutableLiveData<String> finishActObServAble = new MutableLiveData<>();
        private MutableLiveData<HashMap<String,Object>> startPageObServAble = new MutableLiveData<>();

        public MutableLiveData<String> getShowDialogObservable() {
            return showDialogObservable;
        }

        public void setShowDialogObservable(String showDialogObservable) {
            this.showDialogObservable.setValue(showDialogObservable);
        }

        public MutableLiveData<String> getHideDialogObservable() {
            return hideDialogObservable;
        }

        public void setHideDialogObservable(String hideDialogObservable) {
            this.hideDialogObservable.setValue(hideDialogObservable);
        }

        public MutableLiveData<String> getShowMsgObservable() {
            return showMsgObservable;
        }

        public void setShowMsgObservable(String showMsgObservable) {
            this.showMsgObservable.setValue(showMsgObservable);
        }

        public MutableLiveData<String> getFinishActObServAble() {
            return finishActObServAble;
        }

        public void setFinishActObServAble(String finishActObServAble) {
            this.finishActObServAble.setValue(finishActObServAble);


        }

        public MutableLiveData<HashMap<String, Object>> getStartPageObServAble() {
            return startPageObServAble;
        }

        public void setStartPageObServAble(HashMap<String, Object> startPageObServAble) {
            this.startPageObServAble.setValue(startPageObServAble);
        }
    }
}
